package com.lehecai.core.lottery.fetcher.jclq.impl.worker;

import com.lehecai.core.lottery.JclqSPType;
import com.lehecai.core.lottery.LotteryType;
import com.lehecai.core.lottery.fetcher.jclq.AbstractJclqStaticInstantSpFetchWorker;
import com.lehecai.core.lottery.fetcher.jclq.JclqStaticInstantSpItem;
import com.lehecai.core.util.CharsetConstant;
import com.lehecai.core.util.CoreHttpUtils;
import com.lehecai.core.util.CoreJSONUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 通用竞彩篮球固定奖金即时sp抓取实现类
 * @author qatang
 *
 */
public class CommonJclqStaticInstantSpFetchWorkerPengineAPI extends AbstractJclqStaticInstantSpFetchWorker{
	private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
	
	private static final String PROCESS_CODE = "10004"; 
	
	private static final String URL_PENGINE_API = "http://phase.lehecai.com:16041/JclqServlet";
	//private static final String URL_PENGINE_API = "http://172.16.10.195:8080/pengine/JclqServlet";

    @Override
	public List<JclqStaticInstantSpItem> fetchJclqSp(String phaseNo, LotteryType lotteryType, String terminalId){
		if (StringUtils.isBlank(phaseNo)) {
			logger.error("竞彩篮球抓取官方sp时，officialDate为空");
			return null;
		}

		List<String> list;
		try {
            Map<String, String> requestMap = new HashMap<String, String>();
            requestMap.put("ProcessCode", PROCESS_CODE);
            requestMap.put("officialDate", phaseNo);
            requestMap.put("lotteryType", String.valueOf(lotteryType.getValue()));
            if (StringUtils.isNotBlank(terminalId)) {
                requestMap.put("TerminalId", terminalId);
            }
			list = CoreHttpUtils.getUrl(URL_PENGINE_API, requestMap, CharsetConstant.CHARSET_UTF8, 100000);
		} catch (Exception e) {
			logger.error("竞彩篮球抓取官方固定奖金即时sp失败", e);
			return null;
		}
		if (list == null || list.size() == 0) {
			logger.error("竞彩篮球抓取官方固定奖金即时sp失败, lotteryType={}, phase={}", lotteryType, phaseNo);
			return null;
		}
		String matches = list.get(0);
		if (StringUtils.isBlank(matches)) {
			logger.error("未获取到竞彩篮球官方固定奖金即时sp, lotteryType={}, phase={}", lotteryType, phaseNo);
			return null;
		}
		JSONArray jsonArray = JSONArray.fromObject(matches);
		if (jsonArray.isEmpty()) {
			logger.warn("获取到竞彩篮球官方固定奖金即时sp数据为空, 可能没有赛程, lotteryType={}, phase={}", lotteryType, phaseNo);
			return null;
		}
		List<JclqStaticInstantSpItem> jclqStaticInstantSpItems = new ArrayList<JclqStaticInstantSpItem>();
        for (Object aJsonArray : jsonArray) {
            JSONObject object = (JSONObject) aJsonArray;

            if (object == null) {
                return null;
            }

            // 如果指定了彩期，忽略掉返回结果中的非指定彩期的结果
            if (StringUtils.isNotBlank(phaseNo)) {
                String fetchedPhase = CoreJSONUtils.getString(object, "officialDate");
                if (!fetchedPhase.equals(phaseNo)) {
                    continue;
                }
            }

            JclqStaticInstantSpItem item = new JclqStaticInstantSpItem();

            item.setMatchNum(CoreJSONUtils.getString(object, "matchNum"));
            item.setOfficialNum(CoreJSONUtils.getInt(object, "officialNum"));
            item.setOfficialDate(CoreJSONUtils.getDate(object, "officialDate", "yyyyMMdd"));
            item.setMatchName(CoreJSONUtils.getString(object, "matchName"));
            item.setMatchDate(CoreJSONUtils.getDate(object, "matchDate", "yyyyMMddHHmmss"));
            item.setHomeTeam(CoreJSONUtils.getString(object, "homeTeam"));
            item.setAwayTeam(CoreJSONUtils.getString(object, "awayTeam"));

            String spmap = CoreJSONUtils.getString(object, "spmap");
            JSONObject spObj = JSONObject.fromObject(spmap);

            Map<JclqSPType, String> map = new HashMap<JclqSPType, String>();
            if (lotteryType.getValue() == LotteryType.JCLQ_SF.getValue()) {
                map.put(JclqSPType.SF_S, CoreJSONUtils.getString(spObj, "sf_s"));
                map.put(JclqSPType.SF_F, CoreJSONUtils.getString(spObj, "sf_f"));
            } else if (lotteryType.getValue() == LotteryType.JCLQ_RFSF.getValue()) {
                map.put(JclqSPType.RFSF_S, CoreJSONUtils.getString(spObj, "rfsf_s"));
                map.put(JclqSPType.RFSF_F, CoreJSONUtils.getString(spObj, "rfsf_f"));
                map.put(JclqSPType.RFSF_RF, CoreJSONUtils.getString(spObj, "rfsf_rf"));
            } else if (lotteryType.getValue() == LotteryType.JCLQ_SFC.getValue()) {
                map.put(JclqSPType.SFC_KS_1_5, CoreJSONUtils.getString(spObj, "sfc_ks_1_5"));
                map.put(JclqSPType.SFC_ZS_1_5, CoreJSONUtils.getString(spObj, "sfc_zs_1_5"));
                map.put(JclqSPType.SFC_KS_6_10, CoreJSONUtils.getString(spObj, "sfc_ks_6_10"));
                map.put(JclqSPType.SFC_ZS_6_10, CoreJSONUtils.getString(spObj, "sfc_zs_6_10"));
                map.put(JclqSPType.SFC_KS_11_15, CoreJSONUtils.getString(spObj, "sfc_ks_11_15"));
                map.put(JclqSPType.SFC_ZS_11_15, CoreJSONUtils.getString(spObj, "sfc_zs_11_15"));
                map.put(JclqSPType.SFC_KS_16_20, CoreJSONUtils.getString(spObj, "sfc_ks_16_20"));
                map.put(JclqSPType.SFC_ZS_16_20, CoreJSONUtils.getString(spObj, "sfc_zs_16_20"));
                map.put(JclqSPType.SFC_KS_21_25, CoreJSONUtils.getString(spObj, "sfc_ks_21_25"));
                map.put(JclqSPType.SFC_ZS_21_25, CoreJSONUtils.getString(spObj, "sfc_zs_21_25"));
                map.put(JclqSPType.SFC_KS_26, CoreJSONUtils.getString(spObj, "sfc_ks_26"));
                map.put(JclqSPType.SFC_ZS_26, CoreJSONUtils.getString(spObj, "sfc_zs_26"));
            } else if (lotteryType.getValue() == LotteryType.JCLQ_DXF.getValue()) {
                map.put(JclqSPType.DXF_S, CoreJSONUtils.getString(spObj, "dxf_s"));
                map.put(JclqSPType.DXF_F, CoreJSONUtils.getString(spObj, "dxf_f"));
                map.put(JclqSPType.DXF_YS, CoreJSONUtils.getString(spObj, "dxf_ys"));
            }

            item.setSpmap(map);

            jclqStaticInstantSpItems.add(item);
        }
		
		return jclqStaticInstantSpItems;
	}

	@Override
	public List<JclqStaticInstantSpItem> fetchJclqSp(String phaseNo,
			LotteryType lotteryType) {
		return fetchJclqSp(phaseNo, lotteryType, null);
	}
}
